package com.onetoall.yjt;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity{
    private LinearLayout mActivityMain;
    private EditText mEtName;
    private EditText mEtNumber;
    private EditText mEtDeletNo;
    private Button mBtnSave;
    private Button mBtnDel;
    private Button mBtnQuerry;
    private Button mBtnUpdate;
    private TextView tvShow;
    private UserDao dao;
    private UserBean bean;
    private Button btnShop;
//状态 ，手机号，两次签名 手机号+状态一次md5，然后改md5+onetoall进行一次签名
    private void assignViews() {
        mActivityMain = (LinearLayout) findViewById(R.id.activity_main);
        mEtName = (EditText) findViewById(R.id.et_name);
        mEtNumber = (EditText) findViewById(R.id.et_number);
        mEtDeletNo = (EditText) findViewById(R.id.et_delet_no);
        mBtnSave = (Button) findViewById(R.id.btn_save);
        mBtnDel = (Button) findViewById(R.id.btn_del);
        mBtnQuerry = (Button) findViewById(R.id.btn_querry);
        mBtnUpdate = (Button) findViewById(R.id.btn_update);
        tvShow = (TextView) findViewById(R.id.tv);
        dao = new UserDao(MainActivity.this);
        bean = new UserBean();
        btnShop = (Button) findViewById(R.id.btn_shop);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PermissionHelper.requestPermission(this,PermissionHelper.Permission.STORAGE, PermissionHelper.ASK_WRITE_EXTERNAL_STORAGE);
        assignViews();
    }
    public void click(View view){

        switch (view.getId()){
            case R.id.btn_del:
                long a = dao.deleteData(mEtDeletNo.getText().toString());
                Toast.makeText(MainActivity.this,a+"",Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_querry:
                List<UserBean> list = dao.querryBean();
                String tempString = "";
                for (UserBean bean:list) {
                    tempString += bean.getName()+":"+bean.getNumber()+"\n";

                }
                tvShow.setText(tempString);
                break;
            case R.id.btn_update:
               long m =  dao.updataData( mEtDeletNo .getText().toString(),mEtName .getText().toString());
                Toast.makeText(MainActivity.this,m+"",Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_save:
                bean.setName(mEtName.getText().toString());
                bean.setNumber(mEtNumber.getText().toString());
                long i = dao.insertValue(bean.getName(),bean.getNumber());
                Toast.makeText(MainActivity.this,i+"",Toast.LENGTH_LONG).show();
                break;
            case R.id.btn_jump:
                startActivity(new Intent(MainActivity.this,SecondActivity.class));
                break;
            case R.id.btn_ccc:
                startActivity(new Intent(MainActivity.this,RecyActivity.class));
//                textToSpeechDemo.notifyNewMessage("您已经成功的收取了my money in onetoall，0.1314元整");
                break;
            case R.id.btn_gucha:
                startActivity(new Intent(MainActivity.this,GuanChaActivity.class));
                break;
            case R.id.btn_pintu:
                startActivity(new Intent(MainActivity.this,PintuActivity.class));
                break;
            case R.id.btn_zhuashoujihao:
                startActivity(new Intent(MainActivity.this,PhoneNoActivity.class));
                break;
            case R.id.btn_zhifu:
                startActivity(new Intent(MainActivity.this,ZhifuActivity.class));
                break;
            case R.id.btn_shop:


               String mStringUrl = "?mobile=%s&status=%s&sign=%s";
                String sign =  MD5.getMessageDigest((MD5.getMessageDigest((mEtName.getText().toString()+mEtDeletNo.getText().toString()).getBytes())+"onetoall").getBytes());
                mStringUrl = String.format(mStringUrl,mEtName.getText().toString(),mEtDeletNo.getText().toString(),sign);
                String url = "http://beta.yqg.1toall.com.cn/mobile/index.php";
                url=url+mStringUrl;
                startActivity(new Intent(MainActivity.this,ShopingActivity.class).putExtra("url",url));
                Log.e("url",url);
                break;
        }
    }
}
